<?php
/**
 * 统计商铺 收藏、商品收藏、 评论、留言、咨询
 *
 */
class Controller_Admin_Cyclestatis extends Controller_Template
{
	private $_types			= '';
	private $_target_type	= '';
	
	public function __construct($req, $res)
	{
		$this->_types			= Kohana::config('statis.type');
		$this->_target_type		= Kohana::config('statis.target_type');
		$this->template			= 'smarty:admin/home/default';
		parent::__construct($req, $res);
	}

	public function action_index($date = null)
	{
		
		$this->messages($date);

		$this->consult($date);

		$this->favorite($date);
	
		$this->comments($date);
	}

	public function action_cyc()
	{
	
		$start_time	= strtotime('2011-09-02');

		$end_time	= strtotime(date('Y-m-d', time()));

		//echo date('Y-m-d', $s_time);

		//echo date('Y-m-d', $end_time);

		$count	= ($end_time - $start_time)/86400;
	
		
		for($i=0; $i< $count; $i++)
		{

			$start_time = date('Y-m-d', $start_time);
			
			$this->messages($start_time);

			$this->consult($start_time);

			$this->favorite($start_time);

			$this->comments($start_time);

			$start_time	= strtotime("+1 day", strtotime($start_time));

		}

	}

	protected function comments($date = null)
	{
		$arr_time	= $this->get_date($date);

		$start_time = $arr_time['start_time'];

		$end_time	= $arr_time['end_time'];

		$data		= $this->get_comments_data($start_time, $end_time);	

	}

	private function get_comments_data($start_time, $end_time)
	{
		$comments	= new Model_Comments();
	
		$condition	= array('target_type' => 1, 'create_time' => array('min' => $start_time, 'max' => $end_time));

		$data		= $comments->admin_get_comments($condition);

		$target_type= Kohana::config('statis.target_type.shop');

		$type		= Kohana::config('statis.type.comments');

		$rs_data	= $this->formate_comments($data, $target_type, $type, $start_time);

		Model_Admin_Cyclestatis::update_data($rs_data);
	}

	private function formate_comments($data, $target_type, $type, $time)
	{
		$rs	= array();

		$arr_user	= array();

		for($i=0; $i< count($data); $i++)
		{
			$target_id	= $data[$i]['shop_id'];

			if(! isset($rs[$target_id]))
			{
				$rs[$target_id]['target_id']	= $target_id;

				$rs[$target_id]['num']			= 1;

				$rs[$target_id]['target_type']	= $target_type;

				$rs[$target_id]['type']			= $type;

				$rs[$target_id]['time']			= $time;

				$rs[$target_id]['users']		= 1;
			
				$arr_user[$target_id][]			= $data[$i]['uid'];	
			}else{

				$rs[$target_id]['num']          += 1;
				
				if(! in_array($data[$i]['uid'], $arr_user[$target_id]))
				{
					$rs[$target_id]['users']    += 1;
					
					$arr_user[$target_id][]     = $data[$i]['uid'];
				}

			}
		}
		return $rs;	
	}

	protected function favorite($date = null)
	{
		$arr_time	= $this->get_date($date);

		$start_time = $arr_time['start_time'];

		$end_time	= $arr_time['end_time'];

		$goods_data	= $this->get_goods_favorite($start_time, $end_time);

		$shop_data  = $this->get_shop_favorite($start_time, $end_time);

		Model_Admin_Cyclestatis::update_data($goods_data);

		Model_Admin_Cyclestatis::update_data($shop_data);
	}

	private function get_shop_favorite($start_time, $end_time)
	{
		$data		= Model_My_Myfavorite::get_myfavorite_type($type=2, $start_time, $end_time);
		
		$target_type =	Kohana::config('statis.target_type.shop');

		$type		 = Kohana::config('statis.type.favorite');

		$rs_data	= $this->favorite_formate($data, $target_type, $type, $start_time);

		return $rs_data;
	}

	private function get_goods_favorite($start_time, $end_time)
	{
		$data		= Model_My_Myfavorite::get_myfavorite_type($type=1, $start_time, $end_time);

		$target_type =	Kohana::config('statis.target_type.goods');

		$type		 = Kohana::config('statis.type.favorite');

		$rs_data	= $this->favorite_formate($data, $target_type, $type, $start_time);

		return $rs_data;
	}
	private function favorite_formate($data, $target_type, $type, $time)
	{
		$rs	= array();

		$arr_user	= array();

		for($i=0; $i< count($data); $i++)
		{
			$target_id	= $data[$i]['itemid'];

			if(! isset($rs[$target_id]))
			{
				$rs[$target_id]['target_id'] = $target_id;

				$rs[$target_id]['num']		 = 1;

				$rs[$target_id]['target_type']	 = $target_type;

				$rs[$target_id]['type']		 = $type;

				$rs[$target_id]['time']      = $time;

				$rs[$target_id]['users']	 = 1;

				$arr_user[$target_id][]		 = $data[$i]['uid'];

			}else{
				
				$rs[$target_id]['num']       += 1;

				if(! in_array($data[$i]['uid'], $arr_user[$target_id]))
				{
					$rs[$target_id]['users']     += 1;

					$arr_user[$target_id][]      = $data[$i]['uid'];
				}
			}
		}

		return $rs;
	}

	protected function messages($date = null)
	{
		$arr_time	= $this->get_date($date);

		$start_time = $arr_time['start_time'];

		$end_time	= $arr_time['end_time'];

		$type		= Kohana::config('message.type.shop');
		
		$data		= Model_Message::get_all_messages($start_time, $end_time, $type);
		
		$rs_data	= $this->formate_date($data, $start_time, $this->_types['messages'], $this->_target_type['shop']);	
		
		Model_Admin_Cyclestatis::update_data($rs_data);
	}

	
	//咨询
	protected function consult($date = null)
	{
		$arr_time	= $this->get_date($date);

		$start_time = $arr_time['start_time'];

		$end_time	= $arr_time['end_time'];

		$type		= Kohana::config('message.type.goods');
		
		$data		= Model_Message::get_all_messages($start_time, $end_time, $type);
		
		$rs_data	= $this->formate_date($data, $start_time, $this->_types['consult'], $this->_target_type['goods']);

		Model_Admin_Cyclestatis::update_data($rs_data);
	}

	private function formate_date($data, $start_time, $data_type, $target_type)
	{
		$count	= count($data);

		if($count <= 0)
			return array();
		
		$rs		= array();

		$temp	= array();

		for($i=0; $i< $count; $i++)
		{
			$target_id	= $data[$i]['target_id'];

			if(! isset($rs[$target_id]))
			{
				$rs[$target_id]['target_id']	= $target_id;
			
				$rs[$target_id]['type']			= $data_type;

				$rs[$target_id]['target_type']	= $target_type;

				$rs[$target_id]['num']			= 1;

				$rs[$target_id]['users']		= 1;

				$rs[$target_id]['time']			= $start_time;

			}else{

				$rs[$target_id]['num']			+= 1;

				if(! in_array($data[$i]['uid'], $temp[$target_id]['users']))
					$rs[$target_id]['users']	+= 1;
			}
			$temp[$target_id]['users'] []		= $data[$i]['uid'];
		}

		return $rs;
	}
	
	private function get_date($date	= null)
	{

		$use_date	= trim($date);
		
		if($use_date)
		{

			$start_time	= strtotime($use_date);

			$end_time	= strtotime('+1 day', $start_time);
		
		}else{

			$start_time	= strtotime(date('Y-m-d', strtotime('-1 day')));

			$end_time	= strtotime(date('Y-m-d', time()));
		}

		//echo date('Y-m-d', $start_time).'<br/>';
		
		//echo date('Y-m-d', $end_time).'<br/>';

		return array('start_time' => $start_time, 'end_time' => $end_time);
	}

}

?>
